import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox, QFileDialog
from PyQt5 import QtWidgets, QtCore, QtGui
from student import Ui_Form
from stulink import *
from skimage import io
import PIL.Image


class Demo(QWidget, Ui_Form):
    def __init__(self):
        super(Demo, self).__init__()
        self.setupUi(self)
        self.link = STUlink()
        self.link.load_data()
        self.button_init()
        self.lined_init()
        self.file_path = None

    def lined_init(self):
        self.text_id_input.setPlaceholderText('请输入学生学号')
        self.text_name_input.setPlaceholderText('请输入学生姓名')
        self.text_birth_input.setPlaceholderText('请输入学生出生日期')
        self.text_hobby_input.setPlaceholderText('请输入学生兴趣')
        self.cancel_input.setPlaceholderText('请输入学生学号')
        self.search_input.setPlaceholderText('请输入学生学号')
        self.total_num.setText(str(self.link.length))

        self.text_id_input.textChanged.connect(self.check_input_func)
        self.text_hobby_input.textChanged.connect(self.check_input_func)
        self.text_birth_input.textChanged.connect(self.check_input_func)
        self.text_name_input.textChanged.connect(self.check_input_func)
        self.search_input.textChanged.connect(self.check_input_func)
        self.cancel_input.textChanged.connect(self.check_input_func)
        self.text_info_out.textChanged.connect(self.check_input_func)


    def check_input_func(self):
        if self.text_name_input.text() and self.text_id_input.text() and self.text_birth_input.text() and self.text_hobby_input.toPlainText():
            self.button_add.setEnabled(True)
        else:
            self.button_add.setEnabled(False)

        if self.search_input.text():
            self.button_search.setEnabled(True)
        else:
            self.button_search.setEnabled(False)

        if self.cancel_input.text():
            self.button_cancel.setEnabled(True)
        else:
            self.button_cancel.setEnabled(False)

        if self.text_info_out.toPlainText():
            self.button_clear.setEnabled(True)
        else:
            self.button_clear.setEnabled(False)


    def button_init(self):
        self.button_add.setEnabled(False)
        self.button_search.setEnabled(False)
        self.button_cancel.setEnabled(False)
        self.button_clear.setEnabled(False)

        self.button_add.clicked.connect(self.add_data)
        self.button_save.clicked.connect(self.save_data)
        self.button_search.clicked.connect(self.search_data)
        self.button_cancel.clicked.connect(self.cancel_data)
        self.button_sort.clicked.connect(self.sort_id)
        self.button_clear.clicked.connect(self.clear_text)
        self.pic_upload.clicked.connect(self.load_pic)

    def add_data(self):
        name = self.text_name_input.text()
        id = self.text_id_input.text()
        birth = self.text_birth_input.text()
        hobby = self.text_hobby_input.toPlainText()
        find = self.link.searchNodeByid(id)
        if find != None:
            QMessageBox.critical(self, 'wrong', '该学号已存在')
            self.clear_data()
        else:
            img = PIL.Image.open(self.file_path)
            path = 'pic/' + id + '.jpg'
            img.save(path)
            node = Node(name, id, birth, hobby, path)
            self.link.add(node)
            QMessageBox.information(self, 'information', '学生信息添加成功！')
            self.total_num.setText(str(self.link.length))
            self.clear_data()




    def save_data(self):
        self.link.save_data()
        QMessageBox.information(self, 'information', '学生信息保存成功！')

    def search_data(self):
        id = self.search_input.text()
        find = self.link.searchNodeByid(id)
        if find == None:
            QMessageBox.critical(self, 'wrong', '未找到该学生信息')
            self.search_input.clear()
        else:
            data = find.name + " " + str(find.id) + " " + find.birth + " " + find.hobby
            self.text_info_out.append(data)
            img = QtGui.QPixmap(find.path).scaled(self.pic_show1.width(), self.pic_show1.height())
            self.pic_show2.setPixmap(img)


    def cancel_data(self):
        id = self.cancel_input.text()
        find = self.link.searchNodeByid(id)
        if find == None:
            QMessageBox.critical(self, 'wrong', '链表中无此学生信息')
        else:
            self.link.cancle_id(id)
            QMessageBox.information(self, '提示', '已成功删除该学生信息')
        self.cancel_input.clear()
        self.total_num.setText(str(self.link.length))

    def sort_id(self):
        self.link.sort_id()
        QMessageBox.information(self, '提示', '已完成排序')

    def clear_text(self):
        self.text_info_out.clear()
        self.pic_show2.clear()

    def clear_data(self):
        self.text_id_input.clear()
        self.text_name_input.clear()
        self.text_birth_input.clear()
        self.text_hobby_input.clear()
        self.pic_show1.clear()

    def load_pic(self):
        file, _ = QFileDialog.getOpenFileName(self, 'Open File', './', 'JPEG Files(*.jpg);;PNG Files(*.png)')
        img = QtGui.QPixmap(file).scaled(self.pic_show1.width(), self.pic_show1.height())
        self.pic_show1.setPixmap(img)
        self.file_path = file


if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = Demo()
    demo.show()
    sys.exit(app.exec_())